package com.view;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.Socket;
import java.util.HashMap;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class clientPanel extends JPanel implements MouseListener{
	
	JLabel jl_photo;
	JLabel jl_username;
	int photoNum;
	String name;
	String ip;
	FriendsList ztalk;
	
	private HashMap<String,Socket> ip_Socket_Map;
	private HashMap<Socket,Client> chattingMap;
	
	
	public int getPhotoNum(){
		return this.photoNum;
	}
	public String getName(){
		return this.name;
	}
	public String getIp(){
		return this.ip;
	}
	
	public clientPanel(int photoNum,String name,String ip,FriendsList ztalk){
		this.ztalk = ztalk;
		this.photoNum=photoNum;
		this.name=name;
		this.ip = ip;
		
		this.ip_Socket_Map = ztalk.getIp_Socket_Map();
		this.chattingMap = ztalk.getChattingMap();
		
	
		String imagePath = "Images/photoHead" + photoNum + ".png";
		ImageIcon image = new ImageIcon(imagePath);
		jl_photo = new JLabel(image);
		
		jl_username = new JLabel(name);
		jl_username.setFont(new Font(null, Font.ITALIC, 20));
		jl_username.setForeground(Color.orange);
		jl_photo.addMouseListener(this);
		jl_username.addMouseListener(this);
		this.setLayout(null);
		this.add(jl_photo);
		this.add(jl_username);
		jl_photo.setBounds(20, 0, 50, 50);
		jl_username.setBounds( 80, 25, 70, 30);
		
		this.setOpaque(false);
		this.setSize(150, 50);
		
	}
	
//	public void addActionListener(){
//		
//	}
	
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		System.out.println("press me");
		System.out.println(ztalk.getIp_Socket_Map());
		
		
		String clickIP = this.getIp();
		System.out.println(clickIP);
		
		final Socket socket = ip_Socket_Map.get(clickIP);
		SwingUtilities.invokeLater(new Runnable(){
			public void run(){
				if(chattingMap.get(socket) == null){
					Client client = new Client(socket,ztalk);
					chattingMap.put(socket, client);
					client.receive();
				}else{
					chattingMap.get(socket).setVisible(true);
				}
			}
		});
		//new Client3(socket).receive();
		System.out.println("test ");

		System.out.println(socket.getRemoteSocketAddress().toString());
		
		
	}

	
	public void mouseEntered(MouseEvent arg0) {
		
		jl_username.setForeground(Color.blue);
		jl_username.setFont(new Font(null, Font.ITALIC, 25));
	
	}


	public void mouseExited(MouseEvent arg0) {
		jl_username.setForeground(Color.orange);
		jl_username.setFont(new Font(null, Font.ITALIC, 20));
	}
	
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
}
